package ${packageName};

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.util.UUID;

import cn.ysnk.clz.config.annotation.ControllerAnnotation;
import cn.ysnk.clz.interceptor.security.role.RoleEnum;
import com.jfinal.plugin.activerecord.Page;
import cn.ysnk.clz.util.IKSplitWord;
import cn.ysnk.clz.util.PagerUtil;
import com.jfinal.core.Controller;
import java.text.SimpleDateFormat;
import ${entityPackage}.${model.upEntityFileName};
@ControllerAnnotation(url="${controllerUrl}",roleType={RoleEnum.ROLE_ADMIN,RoleEnum.ROLE_MANAGER,RoleEnum.ROLE_USER})
/**
*自动生成${name}的 controller
*/
public class ${model.upControllerFileName} extends Controller {
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	<#if model.hasKeywordsSearch>
    public void index(){
    	int pageNumber = getParaToInt("pageNumber",1);
		int pageSize = 10;
		String keywords = java.net.URLDecoder.decode(getPara("keywords",""));
		List<String> keywordsList = IKSplitWord.splitWord(keywords);
		Page<${model.upEntityFileName}> ${model.entityFileName}List = ${model.upEntityFileName}.dao.findAllPage${model.upEntityFileName}sByKeywords(pageNumber, pageSize,keywordsList);
		PagerUtil page = new PagerUtil();
		page.setCurrentPageNumber(pageNumber);
		page.setElements(${model.entityFileName}List.getList());
		page.setPageSize(pageSize);
		page.setTotalElements(${model.entityFileName}List.getTotalRow());
		setAttr("page", page);
		setAttr("keywords", keywords);
		renderJsp("/${model.baseEntityName}/view.jsp");
	}
	<#else>
    public void index(){
    	int pageNumber = getParaToInt("pageNumber",1);
		int pageSize = 10;
		Page<${model.upEntityFileName}> ${model.entityFileName}List = ${model.upEntityFileName}.dao.findAllPage${model.entityFileName}s(pageNumber, pageSize);
		PagerUtil page = new PagerUtil();
		page.setCurrentPageNumber(pageNumber);
		page.setElements(${model.entityFileName}List.getList());
		page.setPageSize(pageSize);
		page.setTotalElements(${model.entityFileName}List.getTotalRow());
		setAttr("page", page);
		renderJsp("/${model.baseEntityName}/view.jsp");
	}  
    
	</#if>
	
	public void edit${model.upBaseEntityName}(){
		String ${model.baseEntityName}Id = getPara("${model.baseEntityName}Id");
		if(${model.baseEntityName}Id!=null&&!"".equals(${model.baseEntityName}Id))
			setAttr("${model.baseEntityName}Id", ${model.baseEntityName}Id);
		renderJsp("/${model.baseEntityName}/edit.jsp");
	}
	
	public void save${model.upBaseEntityName}(){
		${model.upEntityFileName} ${model.entityFileName} = null;
		String ${model.baseEntityName}Id = getPara("${model.baseEntityName}Id");
<#list model.columns as column>
	<#if column.formField> 
		<#if column.javaType=="String"> 
		String ${column.columnName} = getPara("${column.columnName}");
		<#elseif column.javaType=="Double"> 
		Double ${column.columnName} = Double.parseDouble(getPara("${column.columnName}"));
		<#elseif column.javaType=="Integer"> 
		Integer ${column.columnName} = getParaToInt("${column.columnName}");
		<#elseif column.javaType=="Date"> 
		Date ${column.columnName} = getParaToDate("${column.columnName}");
		</#if>
	</#if>
</#list>
		if("".equals(${model.baseEntityName}Id)){
			${model.entityFileName} = new ${model.upEntityFileName}();
			${model.entityFileName}.set("id",UUID.randomUUID().toString());
		}else{
			${model.entityFileName} = ${model.upEntityFileName}.dao.findById(${model.baseEntityName}Id);
		}
<#list model.columns as column>
	<#if column.formField> 
		${model.entityFileName}.set("${column.columnName}", ${column.columnName});
	<#else>
		<#if column.date>
		${model.entityFileName}.set("${column.columnName}",new Date());
		</#if>
	</#if>
</#list>
		boolean isSuccess = false;
		if("".equals(${model.baseEntityName}Id)){
			isSuccess = ${model.entityFileName}.save();
		}else{
			isSuccess = ${model.entityFileName}.update();
		}
		if(isSuccess){
			renderJson("{\"error\":0,\"message\":\"\"}");
		}else{
			renderJson("{\"error\":1,\"message\":\"操作失败，请重试！\"}");
		}
	}
}
